import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
// 定义类型 获取地址
const types = {
  SET_LOCATION: 'SET_LOCATION',
  SET_ADDRESS: 'SET_ADDRESS',
  ORDER_INFO: 'ORDER_INFO',
  ADD_ADDRESS: 'ADD_ADDRESS',
  ADD_REMAKE: 'ADD_REMAKE',
}

/* state */
const state = {
  location: {},
  address: '',
  orderInfo:null,
  addAddress:null,
  remakeInfo:{
    tableware:'',
    remake:'',
    price:0
  }
}

/* getters */
const getters = {
  location: (state) => state.location,
  address: (state) => state.address,
  orderInfo: (state) => state.orderInfo,
  addAddress: (state) => state.addAddress,
  addRemake: (state) => state.remakeInfo,
}

/* mutations */
const mutations = {
  [types.SET_LOCATION](state, location) {
    if (location) {
      state.location = location
    } else {
      state.location = null
    }
  },
  [types.SET_ADDRESS](state, address) {
    if (address) {
      state.address = address
    } else {
      state.address = ''
    }
  },
  // 结算
  [types.ORDER_INFO](state, orderInfo) {
    if (orderInfo) {
      state.orderInfo = orderInfo
    } else {
      state.orderInfo = ''
    }
  },
  // 收货地址
  [types.ADD_ADDRESS](state, addAddress) {
    if (addAddress) {
      state.addAddress = addAddress
    } else {
      state.addAddress = ''
    }
  },
  // 添加备注
  [types.ADD_REMAKE](state, remakeInfo) {
    if (remakeInfo) {
      state.remakeInfo = remakeInfo
    } else {
      state.remakeInfo = ''
    }
  },
}

/* actions */
const actions = {
  setLocation: ({ commit }, location) => {
    commit(types.SET_LOCATION, location)
  },
  setAddress: ({ commit }, address) => {
    commit(types.SET_ADDRESS, address)
  },
  setOrderInfo: ({ commit }, orderInfo) => {
    commit(types.ORDER_INFO, orderInfo)
  },
  addAddress: ({ commit }, addAddress) => {
    commit(types.ADD_ADDRESS, addAddress)
  },
  addRemake: ({ commit }, remakeInfo) => {
    commit(types.ADD_REMAKE, remakeInfo)
  },
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions,
})
